package region

import (
	"GoTY/api"
	"GoTY/subscriptions"
	"fmt"
	"os"
	"strings"
)

func CreateRegion(userPath string) {
	os.Mkdir(userPath+"/.config/GoTY/", 0755)

	var region []byte
	fmt.Println("Enter your region (US, RU or other)")
	fmt.Scan(&region)
	r := strings.ToUpper(string(region))
	cfg := api.Config{Region: string(r)}

	subs := subscriptions.MarshalChannel(cfg)
	os.WriteFile(userPath+"/.config/GoTY/subs.json", subs, 0755)
}

func InitRegion(userPath string) string {
	var region api.Config
	f, err := os.Open(userPath + "/.config/GoTY/subs.json")
	if err != nil {
		if os.IsNotExist(err) {
			CreateRegion(userPath)
		}
	}
	defer f.Close()

	subscriptions.UnmarshalChannel(userPath+"/.config/GoTY/subs.json", &region)
	return region.Region
}
